package com.itheima.controller;


import com.alibaba.dubbo.config.annotation.Reference;

import com.itheima.POJO.HealthScheme;
import com.itheima.constant.MessageConstant;
import com.itheima.entity.PageResult;
import com.itheima.entity.QueryPageBean;
import com.itheima.entity.Result;
import com.itheima.service.HealthSchemeService;
import org.apache.ibatis.annotations.Param;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import java.util.Map;

@RestController
@RequestMapping("/healthScheme")
public class HealthSchemeController {

    @Reference
    private HealthSchemeService healthSchemeService;

    //因为添加健康方案的表单传过来的是json字符串
    //我们需要用@RequestBody转换为原来的样子
//    @PreAuthorize("hasAuthority('HEALTHSCHEME_ADD')")
    @RequestMapping("/add")
    public Result addHealthScheme(@RequestBody HealthScheme healthScheme) {
        Result result = healthSchemeService.addHealthScheme(healthScheme);
        return result;
    }

    /**
     * 分页查询
     *
     * @param queryPageBean
     * @return
     */
//    @PreAuthorize("hasAuthority('HEALTHSCHEME_QUERY')")
    @RequestMapping("/findpage.do")
    public PageResult findPage(@RequestBody QueryPageBean queryPageBean) {
        /*
        * private Integer currentPage;//页码
          private Integer pageSize;//每页记录数
          private String queryString;//查询条件
          */
        PageResult pageResult = healthSchemeService.findPage(queryPageBean);
        /*
        *  private Long total;//总记录数
           private List rows;//当前页结果
        * */
        return pageResult;
    }

    /**
     * 删除健康方案
     * /scheme/delete.do
     */
 //   @PreAuthorize("hasAuthority('HEALTHSCHEME_DELETE')")
    @RequestMapping(value = "/delete/{id}", method = RequestMethod.GET)
    public Result deleteHealthScheme(@PathVariable(value = "id") Integer id) {
        //前台传过来的是json类型的字符串,要用@RequestBody 将其转为原来的样子
        System.out.println("Controller::" + id);

        try {
            healthSchemeService.deleteHealthScheme(id);
        } catch (RuntimeException e) {
            return new Result(false, e.getMessage());
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, MessageConstant.DELETE_SCHEME_FAIL);
        }

        return new Result(true, MessageConstant.DELETE_SCHEME_SUCCESS);
        /*
        *
        * private boolean flag;//执行结果，true为执行成功 false为执行失败
            private String message;//返回结果信息，主要用于页面提示信息
           private Object data;//返回数据
        * */
    }

    /**
     * 根据id查找scheme
     * @param id
     * @return
     */
    //"/scheme/findschemeByid?id="+row.id
//    @PreAuthorize("hasAuthority('HEALTHSCHEME_QUERY')")
    @RequestMapping("/findHealthSchemeById")
    public Result findHealthSchemeById(Integer id) {
        Result result   =  healthSchemeService.findHealthSchemeById(id);
        return result;
    }

    ///healthScheme/edit.do
//    @PreAuthorize("hasAuthority('HEALTHSCHEME_EDIT')")
    @RequestMapping("/edit")
    public Result updateHealthScheme(@RequestBody HealthScheme healthScheme){
        //前端传过来的是json字符串 需要用@RequestBody注解
        //System.out.println(map);
        try {
            healthSchemeService.updateHealthScheme(healthScheme);
            return  new Result(true,MessageConstant.EDIT_SCHEME_SUCCESS);
        } catch (RuntimeException e) {
            e.printStackTrace();
            return new Result(false,e.getMessage());
        }catch (Exception e){
            e.printStackTrace();
            return new Result(false,"未知错误,请联系18360438631");
        }
    }

    /**
     * 禁用/启用功能
     *      传递多个参数的时候需要加上@Param进行区分
     *      且在SQL语句中的条件顺序 一定和 传递参数的顺序一一对应
     * @param id
     * @param state
     * @return
     */
    @RequestMapping("/forbidden")
    public Result forbiddenById(@Param("id") Integer id, @Param("state") String state){
        try {
            healthSchemeService.forbiddenById(id,state);
            return new Result(true,MessageConstant.FORBIDDEN_SCHEME_SUCCESS);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false,MessageConstant.FORBIDDEN_SCHEME_FAIL);
        }
    }

}
